﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.DataTypes;
using Infrastructure;
using Parser;
using System.ComponentModel;

namespace Controllers
{
    public class DataSetLoaderMovieLens : DataSetLoaderController<Discrete>
    {
        private BackgroundWorker m_bwWorker;
        public string[] m_geners = { "unknown ", " Action ", " Adventure ", "Animation", " Children's ", " Comedy ", " Crime ", " Documentary ", " Drama ", " Fantasy ", "Film-Noir ", " Horror ", " Musical ", " Mystery ", " Romance ", " Sci-Fi ", " Thriller ", " War ", " Western " };

        public DataSetLoaderMovieLens()
        {
            m_bwWorker = null;
        }
        public DataSetLoaderMovieLens(BackgroundWorker bw)
        {
            m_bwWorker = bw;
        }

        /// <summary>
        /// loads data from parser to data set
        /// </summary>
        /// <returns>returns data set with all data in it that was loaded from parser</returns>
        public override DataSet<Discrete> Loader()
        {
            FileReaderParser parser;
            DataSet<Discrete> dataSet = null;

            parser = new FileReaderParser("movieLens.xml");
            dataSet = new DataSet<Discrete>();
            try
            {
                List<List<string>> data = parser.readSourceFile("u.data",m_bwWorker);
                int cItems = data.Count;

                foreach (List<string> item in data)
                {
                   
                    Discrete vote = System.Convert.ToInt32(item[2].Trim());
                    User<Discrete> user = new User<Discrete>(item[0]);
                    Item<Discrete> it = new Item<Discrete>(item[1]);
                    dataSet.addUserItemRating(user, it, vote);

                    m_bwWorker.ReportProgress(1);
                }
                return MovieLensAttributeLoader(dataSet);
            }
            catch (Exception)
            {
                return MovieLensAttributeLoader(dataSet);
            }
        }
        /// <summary>
        /// Load nonstatistical data to data set
        /// </summary>
        /// <param name="data_set"></param>
        private DataSet<Discrete> MovieLensAttributeLoader(DataSet<Discrete> data_set)
        {
            FileReaderParser parser;
            
            parser = new FileReaderParser("movieLensAttributesMovie.xml");

            try
            {
                List<List<string>> data = parser.readSourceFile("u.item",m_bwWorker);
                List<string> columns = parser.getSourceColumns("u.item").ToList();

                m_bwWorker.ReportProgress(1);

                foreach (List<string> item in data)
                {                  

                    Item<Discrete> temp = data_set.getItem(item[0].Trim());
                    if (temp != null)
                    {
                        for (int i = 1; i < columns.Count; i++)
                        {
                            if (item[i].Trim().Equals("0"))
                            {
                                continue;
                            }
                            else
                            {
                                if (i < 4)
                                {
                                    temp.set_data_Attribute(columns[i], item[i]);
                                }
                                else
                                {
                                    temp.set_data_Attribute(columns[i], m_geners[i - 4]);
                                }
                            }
                            m_bwWorker.ReportProgress(1);
                        }
                    }
                }
                return data_set;

            }
            catch (Exception)
            {

                return data_set;
            }

        }
    }
}
